<template>
  <div class="login">
    <van-form @submit="onSubmit">
      <van-field
        v-model="info.phone"
        name="手机号"
        label="手机号"
        placeholder="手机号"
        :rules="[{ required: true, message: '请填写手机号' }]"
      />
      <van-field
        v-model="info.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" @click="login"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      info: {
        phone: "",
        password: "",
      },
    };
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
    },
    login(){
        // 1.获取登录数据
        // 2.验证登录数据  长度 是否为空  过滤特殊符号 ：< where 1=1
        // 3.执行登录数据提交
        this.$api.reqLogin(this.info)
        .then(res=>{
            //提示用户登录成功
            this.$notify({ type: 'success', message: '通知内容' })
            //将用户信息缓存到本地一份
            localStorage.setItem('memberInfo',JSON.stringify(res.data.list))
            //跳转到首页
            let loginStatus= this.$router.push('/')
            console.log(loginStatus)
        })
        .catch(err=>console.log(err))
    }
  },
};
</script>

<style scoped>
.login {
  margin: 4rem auto;
}
</style>
